package com.ruoyi.gateway.filter;

import java.net.URI;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 自定义局部过滤器，啥都不做，发现每间隔30秒走一次apply方法。
 */
@Component
public class TestLocalFilter extends AbstractGatewayFilterFactory<Object> {

    @Override
    public String name() {
        return "TestLocalFilter";
    }

    @Override
    public GatewayFilter apply(Object config) {
        //System.out.println(new Date() + "-------------------TestLocalFilter");
        //返回Gateway对象
        return (exchange, chain) -> {
/*            //MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
            ////获取参数
            //System.out.println(queryParams.get("a"));
            //System.out.println("你好");
            //
            ////获取URI
            //URI newUri = exchange.getRequest().getURI();
            //String rawPath = newUri.getRawPath();
            //System.out.println(rawPath);
            //
            //rawPath = rawPath + "/2";
            ////修改URI
            //ServerHttpRequest build = exchange.getRequest().mutate().path(rawPath).build();
            //
            //return chain.filter(exchange.mutate().request(build).build());*/
            return chain.filter(exchange);
        };
    }
}